package com.embracesource.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.embracesource.monitor.dto.ServerDTO;
import com.embracesource.monitor.entity.Server;
import com.embracesource.monitor.mapper.ServerMapper;
import com.embracesource.monitor.service.IServerService;
import com.gnerv.pabu.basic.core.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 基础平台-服务器表 服务实现类
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-09-03
 */
@Service
public class ServerServiceImpl extends ServiceImpl<ServerMapper, Server> implements IServerService {

    @Resource
    private ServerMapper serverMapper;

    @Override
    public boolean saveServer(Server server) {
        if (StringUtils.isEmpty(server.getUkId())) {
            createServer(server);
        } else {
            updateServer(server);
        }
        return true;
    }

    @Override
    public boolean createServer(Server server) {
        server.setUkId(UUIDUtils.getLower());
        serverMapper.insert(server);
        return true;
    }

    @Override
    public boolean deleteServer(String serverId) {
        serverMapper.deleteById(serverId);
        return true;
    }

    @Override
    public boolean updateServer(Server server) {
        serverMapper.updateById(server);
        return true;
    }

    @Override
    public ServerDTO selectServer(String serverId) {
        ServerDTO server = serverMapper.selectServer(serverId);
        return server;
    }

    @Override
    public Page<ServerDTO> listServer(Server server, Long current, Long size) {
        Page<ServerDTO> servers = serverMapper.listServer(new Page<>(current, size), server);
        return servers;
    }
}
